Online-Academy
Look, Read, Understand, Apply

Inheritance - i

Inheritance is the method of acquiring properties of existing class (base class) by new class (sub class). object is the topmost class in Python. Every class inherits from object.
In python, by passing name of base class to sub class, inheritance is done.

class Car(Vehicle)
Here, Car is derived (or sub) class and Vehicle is base (or super class).

Sub class aquires all properties of base class, it can add its additional properties and methods also.

Save Vehicle class in vehicle.py
class Vehicle:
    model:str = None
    def __init__(self,name,price,mod):
        self.name = name
        self.price = price
        self.model = mod
    def get_name(self):
        return self.name
    def get_price(self):
        return self.price
    def get_model(self):
        return self.model
    def show(self):
        print("show mes")
Importing Vehicle class
Here, both classes are stored in same folder
from vehicle import Vehicle
class car(Vehicle):
    def set(self,color):
        self.color = color
    def show_car(self):
        print(f"Name: {self.name}, color: {self.color}")
if __name__== "__main__":
    v = Vehicle("Volvo",4000,"i30")
    print(f"{v.get_name()}, {v.get_price()}")
    v.name = "Hyunda"

    v.model = "i20"
    print(f"{v.name}, {v.model}")
    Vehicle.name = "Mercedes"
    Vehicle.price = 300000
    Vehicle.model = "Scorpion"
    print(f"{Vehicle.name}, {Vehicle.price}")
    #Vehicle.show()
    c = car("abc",3000,"i30")
    c.set("red")
    c.show()
    c.show_car()